/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gadglet.client.gwt.core.ui;

import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.client.gwt.core.GadgletResponse;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.gadglet.client.gwt.ui.UIUtils;
import com.gadglet.params.ProfPrivacyStatus;
import com.gadglet.params.ProfileFields;
import com.gadglet.params.ReqActionTypes;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;


/**
 * UI class used to present a list of friends define for the user
 * on this gadget service
 *
 */
public class RemoteFriendsPanel {
DebugDialogBox debug = DebugDialogBox.getErrorNotifier();
	
	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
	
	private final VerticalPanel remoteFrindsContainer = new VerticalPanel();
	private final VerticalPanel remoteFrindsList = new VerticalPanel();

		
	final GadgletQuery viewFriendsQry = new GadgletQuery();

	final ViewFriendsRequest viewFriendsRequest = new ViewFriendsRequest(
			viewFriendsQry);
		
		
	private static RemoteFriendsPanel remoteFriendsPanel = null;
	
	public VerticalPanel getRemoteFrindsContainer(){
		return this.remoteFrindsContainer;
	}
		
	
	public static RemoteFriendsPanel getRemoteFriendsPanel() {
		if (remoteFriendsPanel == null) {
			remoteFriendsPanel = new RemoteFriendsPanel();
		}
		return remoteFriendsPanel;
	}
	
	RemoteFriendsPanel(){
		
		remoteFrindsContainer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		Label remoteFrindsLabel = new Label();
		remoteFrindsLabel.setWidth("250px");
		remoteFrindsLabel.setStylePrimaryName("friendsLabel");
		remoteFrindsLabel.setText(prefs.getMsg("gadgetLabelGadgetFriends") );
		
		remoteFrindsContainer.add(remoteFrindsLabel);
		remoteFrindsContainer.add(remoteFrindsList);
			
	}
	
	public void viewRemoteFriends(){
		remoteFrindsList.clear();
		viewFriendsQry.setRequestAction(ReqActionTypes.GET_FRIENDS);
		viewFriendsRequest.makeRequest();
		
	}
	

	
	
	public class ViewFriendsRequest extends GadgletRequest {
		ViewFriendsRequest(GadgletQuery req) {
			super(req);
		}

		@Override
		protected void processResults(GadgletResponse jResponse) {

			if (jResponse.isSuccessful()) {
	
				int inviteNum = jResponse.getRootItemsNum();
				String nickName ;
				

				for (int index = 0; index < inviteNum; index++) {
					JSONObject item = jResponse.getItem(index);
								
					String photo = null;
					
					if(item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName())!=null)
						photo = item.get(ProfileFields.PROFILE_THUMBNAILURL.getParamName()).isString().stringValue();
					
					if( item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue().equals(ProfPrivacyStatus.PRIVATE.getPrivacyStatus()))
						nickName = prefs.getMsg("gadgetLabelPrivateProfile") ;
					else
						nickName = item.get(ProfileFields.PROFILE_NICKNAME.getParamName()).isString().stringValue();

					try {
						RemoteFriend remoteFriend = new RemoteFriend(nickName,photo);

						remoteFrindsList
								.add(remoteFriend.getRemoteFriendItem());
					} catch (Exception e) {
						debug.showError(111, e.getMessage());
					}
				}

			} else
				debug.showError(101, jResponse.getError());
			
			
		}
	}
	
	
	private class RemoteFriend {

	
		private String invitedByNickName;
		
		private String friendPhoto;

		public RemoteFriend(
				String invitedByNickName, String photo) {
			this.friendPhoto = photo;

			this.invitedByNickName = invitedByNickName;
		}


		HorizontalPanel getRemoteFriendItem() {
			HorizontalPanel item = new HorizontalPanel();
			item.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
			item.setHeight("40px");
			item.add(new HTML(invitedByNickName));
			item.add(UIUtils.getSpace());
			
			if(this.friendPhoto!=null){
				Image img = new Image(this.friendPhoto);
				img.setPixelSize(30, 30);
					item.add(img);
			}
			return item;
		}

	}
	

}
